<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加商品种类</title>
    <script src="asserts/plugins/jquery/jquery-3.6.0.min.js"></script>
    <script src="asserts/plugins/bootstrap/js/bootstrap.js"></script>
    <link href="asserts/plugins/bootstrap/css/bootstrap.css" rel="stylesheet">
    <script>
        $(document).ready(function() {
            // 表单提交事件
            $('#addGoodsTypeForm').on('submit', function(event) {
                event.preventDefault(); // 阻止表单默认提交
                var formData = {
                    goodsParent: $('select[name="goodsParent"]').val(),
                    typename: $('input[name="typename"]').val(),
                    desc: $('textarea[name="desc"]').val()
                };

                // AJAX请求添加商品分类
                $.ajax({
                    type: 'POST',
                    url: '/admin/AdminController',
                    data: {
                        type: 'addGoodsClass',
                        data: JSON.stringify(formData)
                    },
                    success: function(response) {
                        if (response.status === 200) {
                            alert('商品种类添加成功');
                            // 刷新页面或重定向到其他页面
                        } else {
                            alert('商品种类添加失败: ' + response.message);
                        }
                    },
                    error: function(xhr, status, error) {
                        alert('商品种类添加失败: ' + error);
                    }
                });
            });

            // 页面加载时获取商品种类列表并渲染select选项
            $.ajax({
                type: 'GET',
                url: '/admin/AdminController',
                dataType: 'json',
                success: function(data) {
                    var select = $('select[name="goodsParent"]');
                    select.empty(); // 清空之前的选项
                    select.append('<option value="1">--请选择--</option>');
                    $.each(data, function(index, type) {
                        if (type.level <= 2) {
                            select.append('<option value="' + type.id + '">' + type.name + '</option>');
                        }
                    });
                },
                error: function(xhr, status, error) {
                    alert('获取商品种类列表失败: ' + error);
                }
            });
        });
    </script>
</head>
<body>
<div style="width:98%;margin-left: 1%;" class="container">
    <div class="panel panel-default">
        <div class="panel-heading">
            添加商品种类
        </div>
        <div class="panel-body">
            <form id="addGoodsTypeForm">
                <div class="row">
                    <div class="form-group form-inline">
                        <span>所属种类</span>
                        <select name="goodsParent" class="form-control">
                        </select>
                    </div>
                </div>
                <div class="row">
                    <div class="form-group form-inline">
                        <span>种类名称</span>
                        <input type="text" name="typename" class="form-control">
                    </div>
                </div>
                <!--类别描述-->
                <div class="row">
                    <div class="form-group form-inline">
                        <span>种类描述</span>
                        <textarea name="desc" class="form-control" rows="5"></textarea>
                    </div>
                </div>
                <div class="row">
                    <div class="btn-group">
                        <button type="reset" class="btn btn-default">清空</button>
                        <button type="submit" class="btn btn-default">添加</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
</html>


<!---->
